﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Data.AID;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Business.AID;
using Business.AID.Extensions;
using System.Linq;
using Microsoft.SharePoint.Administration;

namespace Presentation.AID.WebPart.Presentation.AID.VWPGerenciarExcecoes
{
    public partial class VWPGerenciarExcecoesUserControl : UserControl
    {
        private int IdColaborador
        {
            get { return (int)ViewState["IdColaborador"]; }
            set { ViewState["IdColaborador"] = value; }
        }

        protected int SelectedItem = 0;
        protected bool UpdateListProperties = false;

        protected void Page_PreRender(object sender, EventArgs e)
        {
            SelecionaColaborador();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SPUser user = SPContext.Current.Web.CurrentUser;
                List<Excecao> ex = new ExcecaoBO().GetAllExceptions();
            }
        }

        protected void ddlUsuarios_SelectedIndexChanged(object sender, EventArgs e)
        {
            int.TryParse(ddlUsuarios.SelectedValue, out SelectedItem);
            UpdateListProperties = true;
        }
        private void SelecionaColaborador()
        {
            if ((ddlUsuarios.SelectedIndex > -1) & (!UpdateListProperties))
            {
                int.TryParse(ddlUsuarios.SelectedValue, out SelectedItem);
            }

            ddlUsuarios.Items.Clear();
            ColaboradorBO colaboradorBO = new ColaboradorBO();
            List<Colaborador> colaboradores = colaboradorBO.GettAllEmployees().Where(p => p.Matricula != "").ToList();
            ddlUsuarios.AppendDataBoundItems = true;
            ddlUsuarios.DataSource = colaboradores;
            ddlUsuarios.DataValueField = "Id";
            ddlUsuarios.DataTextField = "Nome";
            ddlUsuarios.DataBind();

            if (SelectedItem != 0)
            {
                ddlUsuarios.Items.FindByValue(SelectedItem.ToString()).Selected = true;
            }

            if (UpdateListProperties)
            {
                Colaborador colaborador = colaboradores.FirstOrDefault(p => p.Id == SelectedItem);
                
                IdColaborador = colaborador.Id;
                lblNome.Text = colaborador.Nome;
                lblEmail.Text = colaborador.Email;
                lblLogin.Text = colaborador.Login;
            }
        }

        protected void btnConcederPermissao_Click(object sender, EventArgs e)
        {
            Colaborador colaborador = new ColaboradorBO().GetEmployeeById(IdColaborador);

            //SPUser user = SPContext.Current.Web.Users["oluiz-modulo"];
            SPUserCollection userCollection = SPContext.Current.Web.AllUsers;
            
            //bool isMember = user.InGroup(group);

            //if (isMember)
            //{

            //}
            //else
            //{

            //}

        }
    }
}
